//
//  KLMCommonButton.swift
//  KLM_GN
//
//  Created by Nic on 2025/3/6.
//

import UIKit

// 定义按钮样式结构体
struct ButtonStyle {
    var cornerRadius: CGFloat
    var backgroundColor: UIColor
    var backgroundImage: UIImage?
    var borderColor: UIColor
    var borderWidth: CGFloat
    var textColor: UIColor
    var font: UIFont?

    // 初始化方法，提供默认值
    init(cornerRadius: CGFloat = 16,
         backgroundColor: UIColor = rgba(19, 207, 128, 1),
         backgroundImage: UIImage? = nil,
         borderColor: UIColor = .clear,
         borderWidth: CGFloat = 0,
         textColor: UIColor = .white,
         font: UIFont? = UIFont(safeName: "PingFangSC-Medium", size: 16)) {
        self.cornerRadius = cornerRadius
        self.backgroundColor = backgroundColor
        self.backgroundImage = backgroundImage
        self.borderColor = borderColor
        self.borderWidth = borderWidth
        self.textColor = textColor
        self.font = font
    }
}

class KLMCommonButton: UIButton {
    // 存储不同状态下的按钮样式
    private(set) lazy var normalStyle: ButtonStyle = {
        ButtonStyle(backgroundColor: rgba(19, 207, 128, 1))
    }()
    private(set) lazy var selectedStyle: ButtonStyle = {
        ButtonStyle(backgroundColor: rgba(19, 207, 128, 0.3))
    }()
    private(set) lazy var highlightedStyle: ButtonStyle = {
        ButtonStyle(backgroundColor: rgba(246, 246, 246, 1))
    }()
    private(set) lazy var disabledStyle: ButtonStyle = {
        ButtonStyle(backgroundColor: rgba(19, 207, 128, 0.3))
    }()
    
    // 初始化方法，允许传入不同状态的样式
    init(frame: CGRect,
         normalStyle: ButtonStyle? = nil,
         selectedStyle: ButtonStyle? = nil,
         highlightedStyle: ButtonStyle? = nil,
         disabledStyle: ButtonStyle? = nil) {
        super.init(frame: frame)
        if let normal = normalStyle {
            self.normalStyle = normal
        }
        if let selected = selectedStyle {
            self.selectedStyle = selected
        }
        if let highlighted = highlightedStyle {
            self.highlightedStyle = highlighted
        }
        if let disabled = disabledStyle {
            self.disabledStyle = disabled
        }
        commonInit()
    }

    required init?(coder: NSCoder) {
        // 默认样式
        super.init(coder: coder)
        commonInit()
    }
    
    private func commonInit() {
        // 应用正常状态样式
        applyStyle(style: normalStyle, for: .normal)
        applyStyle(style: selectedStyle, for: .selected)
        // 应用高亮状态样式
        applyStyle(style: highlightedStyle, for: .highlighted)
        // 应用禁用状态样式
        applyStyle(style: disabledStyle, for: .disabled)

        // 监听按钮状态变化
        addTarget(self, action: #selector(buttonStateChanged), for: [.touchDown, .touchUpInside, .touchUpOutside, .touchCancel])
    }

    func updateStyle(state:UIControl.State = .normal, cornerRadius: CGFloat = 16,
         backgroundColor: UIColor = rgba(19, 207, 128, 1),
         backgroundImage: UIImage? = nil,
         borderColor: UIColor = .clear,
         borderWidth: CGFloat = 0,
         textColor: UIColor = .white,
         font: UIFont? = UIFont(safeName: "PingFangSC-Medium", size: 16)) {
        
        applyStyle(style: ButtonStyle(cornerRadius: cornerRadius, backgroundColor: backgroundColor, backgroundImage: backgroundImage, borderColor: borderColor, borderWidth: borderWidth, textColor: textColor, font: font), for: state)
    }
    // 应用样式到指定状态
    private func applyStyle(style: ButtonStyle, for state: UIControl.State) {
        layer.cornerRadius = style.cornerRadius
        layer.borderWidth = style.borderWidth
        layer.borderColor = style.borderColor.cgColor
        layer.masksToBounds = true

        if let backgroundImage = style.backgroundImage {
            setBackgroundImage(backgroundImage, for: state)
        } else {
            setBackgroundColor(style.backgroundColor, for: state)
        }

        setTitleColor(style.textColor, for: state)
        titleLabel?.font = style.font
    }

    // 设置按钮背景颜色
    private func setBackgroundColor(_ color: UIColor, for state: UIControl.State) {
        UIGraphicsBeginImageContext(CGSize(width: 1, height: 1))
        if let context = UIGraphicsGetCurrentContext() {
            context.setFillColor(color.cgColor)
            context.fill(CGRect(x: 0, y: 0, width: 1, height: 1))
            let colorImage = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()
            setBackgroundImage(colorImage, for: state)
        }
    }

    // 按钮状态变化时的处理方法
    @objc private func buttonStateChanged() {
        switch state {
        case .normal:
            applyStyle(style: normalStyle, for: .normal)
        case .selected:
            applyStyle(style: selectedStyle, for: .selected)
        case .highlighted:
            applyStyle(style: highlightedStyle, for: .highlighted)
        case .disabled:
            applyStyle(style: disabledStyle, for: .disabled)
        default:
            break
        }
    }
}
